package com.chelun.support.clmutiplyfragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chelun.support.clutils.utils.DipUtils;


public class ClMultiYFootView extends LinearLayout {

    private Context context;

    private LinearLayout footView;

    private RecyclerView mRecyclerView;

    private TextView footTipsTextView;

    private ProgressBar footProgressBar;

    private OnMoreListener mOnMoreListener;

    private boolean isClickRefreshMore = false;

    private boolean isFootViewHide = false;

    private boolean isRefreshing = false;

    private boolean isAutoRefreshMore = true;


    public void setRecyclerView(RecyclerView listView) {
        this.mRecyclerView = listView;

        this.mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (isAutoRefreshMore && !isRefreshing
                        && recyclerView.getChildLayoutPosition(recyclerView.getChildAt(recyclerView.getChildCount() - 1)) == recyclerView.getAdapter().getItemCount() - 1
                        && newState == RecyclerView.SCROLL_STATE_IDLE) {
                    refreshMore();
                    if (mOnMoreListener != null) {
                        mOnMoreListener.getMore();
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            }
        });
    }

    public ClMultiYFootView(Context context, int res) {
        super(context);
        this.context = context;
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        footView = (LinearLayout) layoutInflater.inflate(
                R.layout.clmulti_widget_list_y_foot_view, null);

        footView.setLayoutParams(new android.view.ViewGroup.LayoutParams(context.getResources().getDisplayMetrics().widthPixels, LayoutParams.WRAP_CONTENT));
        addView(footView);
        footView.setVisibility(View.GONE);

        footTipsTextView = (TextView) footView.findViewById(R.id.clMultimoreTextView);
        footProgressBar = (ProgressBar) footView
                .findViewById(R.id.clMultimoreProgress);
        footView.findViewById(R.id.clMultibg).setBackgroundResource(res);

        footView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isClickRefreshMore) {
                    refreshMore();
                    if (mOnMoreListener != null) {
                        mOnMoreListener.getMore();
                    }
                }
            }
        });
    }

    public ClMultiYFootView(Context context, int res, RecyclerView recyclerView) {
        this(context, res);
        setRecyclerView(recyclerView);
    }

    public ClMultiYFootView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 分割线宽时，调用
     */
    public void setFootViewBackground(int resid) {
        footView.setBackgroundResource(resid);
        footView.setPadding(0, DipUtils.dip2px(15), 0,
                DipUtils.dip2px(15));
    }

    /**
     * 设置拉去更多回调
     *
     * @param mOnMoreListener
     */
    public void setOnMoreListener(OnMoreListener mOnMoreListener) {
        this.mOnMoreListener = mOnMoreListener;
    }

    /**
     * 一开始需要底部显示刷新时调用
     */
    public void refreshMore() {
        isRefreshing = true;
        footTipsTextView.setText("正在加载更多");
        footProgressBar.setVisibility(VISIBLE);
        footView.setVisibility(View.VISIBLE);

        if (isFootViewHide && mRecyclerView != null) {
            setVisibility(VISIBLE);
            isFootViewHide = false;
        }
    }

    public void refreshMoreOver(String msg, boolean isClickRefreshMore) {
        refreshMoreOver(msg, isClickRefreshMore, !isClickRefreshMore);
    }

    /**
     * 更换底部提示信息 isClickRefreshMore 底部是否可点击 isAutoRefreshMore是否可自动刷新
     */
    public void refreshMoreOver(String msg, boolean isClickRefreshMore, boolean isAutoRefreshMore) {
        footTipsTextView.setText(msg);
        footProgressBar.setVisibility(View.GONE);
        footView.setVisibility(View.VISIBLE);
        if (!isClickRefreshMore) {
            footView.setEnabled(false);
        } else {
            footView.setEnabled(true);
        }
        this.isAutoRefreshMore = isAutoRefreshMore;
        this.isClickRefreshMore = isClickRefreshMore;
        isRefreshing = false;

        if (isFootViewHide && mRecyclerView != null) {
            footView.setVisibility(VISIBLE);
            isFootViewHide = false;
//            if (getLayoutParams() != null)
//                mRecyclerView.getAdapter().notifyItemChanged(mRecyclerView.getChildAdapterPosition(this));
        }
    }

    /**
     * 普通拉取更多时调用
     */
    public void refreshMoreOver() {
        refreshMoreOver("查看更多", true);
    }

    /**
     * 普通拉取更多时调用
     */
    public void refreshMoreOver(boolean isClickRefreshMore) {
        refreshMoreOver("查看更多", isClickRefreshMore);
    }

    /**
     * 移除底部时调用
     */
    public void refreshMoreOverHideFoot() {
        isClickRefreshMore = false;
        isAutoRefreshMore = false;
        isRefreshing = false;

        if (!isFootViewHide && mRecyclerView != null) {
            footView.setVisibility(GONE);
            isFootViewHide = true;
//            if (getLayoutParams() != null)
//                mRecyclerView.getAdapter().notifyItemChanged(mRecyclerView.getChildAdapterPosition(this));
        }
    }

    public boolean isHide() {
        return isFootViewHide;
    }

    public interface OnMoreListener {
        void getMore();
    }
}
